package com.zheng.common.util;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 日期时间工具类
 * @author lvb
 * @date 2017-07-05
 * @version V1.0
 * @Copyright:
 */
public class DateUtil {

	public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String FORMAT_YYYY_MM = "yyyy-MM";
	public static final String FORMAT_YYYY = "yyyy";
	public static final String FORMAT_YYYYMMDDHHMMSSSSS = "yyyyMMddhhmmssSSS";
	public static final String FORMAT_YYYYMMDDHHMMSS = "yyyyMMddhhmmss";

	public static final String FORMAT_YY_MM_DD_HH_MM_SS = "yy-MM-dd HH:mm:ss";
	public static final String FORMAT_YY_MM_DD = "yy-MM-dd";
	public static final String FORMAT_YY_MM = "yy-MM";
	public static final String FORMAT_YY = "yy";
	public static final String FORMAT_MM = "MM";
	public static final String FORMAT_DD = "dd";
	public static final String FORMAT_YYMMDDHHMMSSSSS = "yyMMddhhmmssSSS";
	public static final String FORMAT_YYMMDDHHMMSS = "yyMMddhhmmss";

	public static String getCurrentDate(String formatPattern) {
		formatPattern = StringUtils.isBlank(formatPattern) ? FORMAT_YYYYMMDDHHMMSS : formatPattern;
		SimpleDateFormat format = new SimpleDateFormat(formatPattern);
		return format.format(new Date());
	}
	public static Date getNowDate() {
		return new Date();
	}

	public static String getSeqString() {
		SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddHHmmss"); // "yyyyMMdd G
		return fm.format(new Date());
	}

	public static Timestamp getCurrentTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 获取当前时间，格式为 yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static String getCurrentTimeStr(String format) {
		format = StringUtils.isBlank(format) ? FORMAT_YYYY_MM_DD_HH_MM_SS : format;
		Date now = new Date();
		return date2Str(now, format);
	}

	public static String date2Str(Date date) {
		return date2Str(date, FORMAT_YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 时间转换成 Date 类型
	 *
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Str(Date date, String format) {
		if ((format == null) || format.equals("")) {
			format = FORMAT_YYYY_MM_DD_HH_MM_SS;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date != null) {
			return sdf.format(date);
		}
		return "";
	}

	/**
	 * 获取批量付款预约时间
	 * @return
	 */
	public static String getRevTime() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, 1);
		String dateString = new SimpleDateFormat(DateUtil.FORMAT_YYYYMMDDHHMMSS).format(cal.getTime());
		System.out.println(dateString);
		return dateString;
	}

	/**
	 * 时间比较
	 * @param date1
	 * @param date2
	 * @return DATE1>DATE2返回1，DATE1<DATE2返回-1,等于返回0
	 */
	public static int compareDate(String date1, String date2, String format) {
		DateFormat df = new SimpleDateFormat(format);
		try {
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 把给定的时间减掉给定的分钟数
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date minusDateByMinute(Date date, int minute) {
		Date newDate = new Date(date.getTime() - (minute * 60 * 1000));
		return newDate;
	}
	/**
	 * 把给定的时间减掉给定的秒数
	 * @param date
	 * @param second
	 * @return
	 */
	public static Date minusDateBySecond(Date date, int second) {
		Date newDate = new Date(date.getTime() - (second * 1000));
		return newDate;
	}
	public static java.util.Date getDateTimeFromString(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getOnlyStringDateWeekAgo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date currentDate = new Date();
		long weekAgo = (currentDate.getTime() / 1000) - 60 * 60 * 24 * 7;
		currentDate.setTime(weekAgo * 1000);

		return sdf.format(currentDate);
	}

	public static String getDBFomatDate(String date) {
		String[] tempDate = date.split("/");
		date = tempDate[2] + "-" + tempDate[0] + "-" + tempDate[1];
		return date;
	}

	public static String getCSTtoNumberDate(String stringDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(
				"EEE MMM ddHH:mm:ss 'CST' yyyy", Locale.US);
		Date date = null;
		try {
			date = sdf.parse(stringDate);
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sdf.format(date);

	}

	// 获取下一周的日期

	public static  Date nextWeek(Date currentDate) {

		GregorianCalendar cal = new GregorianCalendar();

		cal.setTime(currentDate);

		cal.add(GregorianCalendar.DATE, 7);// 在日期上加7天

		return cal.getTime();

	}

	// 获取本周日的日期

	public static  Date getSunday(Date monday) {

		GregorianCalendar cal = new GregorianCalendar();

		cal.setTime(monday);

		cal.add(GregorianCalendar.DATE, 6);// 在日期上加6天

		return cal.getTime();

	}

	// 获取下一月的日期

	public static  Date nextMonth(Date currentDate) {

		GregorianCalendar cal = new GregorianCalendar();

		cal.setTime(currentDate);

		cal.add(GregorianCalendar.MONTH, 1);// 在月份上加1

		return cal.getTime();

	}

	// 获取上一个月的日期
	public static  Date previousMonth(Date currentDate) {
		GregorianCalendar cal = new GregorianCalendar();

		cal.setTime(currentDate);

		cal.add(GregorianCalendar.MONTH, -1);// 在月份上加1

		return cal.getTime();
	}

	/**
	 * 得到某年某月的第一天
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static  Date getFirstDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
		return cal.getTime();
	}

	/**
	 * 得到某年某月的最后一天
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, value);
		return cal.getTime();
	}

	/**
	 * 获取两个时间间隔时长，自动转换为 x天 x小时 x分 格式
	 * @return
	 */
	public static String covertIntervalDDHHMI(Date sd,Date ed){
		long time = ed.getTime() - sd.getTime();
		Integer[] dhm = getIntervalDHMValue(time);
		return dhm[2]+"天 "+ dhm[1] + "小时 "+dhm[0] + "分";
	}

	/**
	 * 转换成 分，小时，天 的整数数组
	 * @param time
	 * @return
	 */
	public static Integer[] getIntervalDHMValue(long time){
		if(time<0){
			return new Integer[]{0,0,0};
		}
		int minute = Math.round(time/1000/60);
		int hour = Math.round(minute/60);
		int day = Math.round(hour/24);
		return new Integer[]{minute%60,hour%24,day};
	}

	/**
	 * 获取间隔分钟
	 * @param time
	 * @return
	 */
	public static double getIntervalMinute(long time){
		return Math.ceil(time/1000/60);
	}

	/**
	 * 获取间隔小时
	 * @param time
	 * @return
	 */
	public static double getIntervalHour(long time){
		return Math.ceil(time/1000/60/60);
	}

	/**
	 * 获取间隔天
	 * @param time
	 * @return
	 */
	public static double getIntervalDay(long time){
		return Math.ceil(time/1000/60/60/24);
	}
	/**
	 * 得到某年某月的某一周的第一天 author:wsr
	 */
	public static  Date getFirstDayOfWeek(int year, int month, int week) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		for (int i = 1; i < 8; i++) {
			cal.set(Calendar.DATE, i);
			if (cal.get(Calendar.DAY_OF_WEEK) - 1 == 1) {// 判断当前天如果是周一
				break;
			}
		}
		cal.set(Calendar.DATE, ((week - 1) * 7) + cal.get(Calendar.DATE));
		return cal.getTime();
	}

	public static  Date getFirstDayOfWeek2(int year, int month, int week) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DATE, ((week - 1) * 7) + 1);
		return cal.getTime();
	}

	/*
	 * 得到某年某月的某一周的最后一天 author:wsr
	 */
	public static  Date getLastDayOfWeek(int year, int month, int week) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		for (int i = 1; i < 8; i++) {
			cal.set(Calendar.DATE, i);
			if (cal.get(Calendar.DAY_OF_WEEK) - 1 == 1) {// 判断当前天如果是周一
				break;
			}
		}
		cal.set(Calendar.DATE, ((week - 1) * 7) + cal.get(Calendar.DATE) + 6);
		return cal.getTime();
	}

	public static Date getLastDayOfWeek2(int year, int month, int week) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		int day = cal.getActualMaximum(Calendar.DATE);
		if (week == 4) {
			cal.set(Calendar.DATE, day);
		} else {
			cal.set(Calendar.DATE, week * 7);
		}
		return cal.getTime();
	}

	// 获取下一年的日期

	public static Date nextYear(Date currentDate) {

		GregorianCalendar cal = new GregorianCalendar();

		cal.setTime(currentDate);

		cal.add(GregorianCalendar.YEAR, 1);// 在年上加1

		return cal.getTime();

	}

	/**
	 * 时间。是否在此范围内 不在返回真 在。假
	 *
	 * @param date
	 *            时间
	 * @param fromDate
	 *            开始时间
	 * @param toDate
	 *            结束时间
	 * @return
	 */
	public static Boolean compare(Date date, Date fromDate, Date toDate) {
		Calendar date1 = Calendar.getInstance();
		date1.setTime(date);
		Calendar from = Calendar.getInstance();
		from.setTime(fromDate);
		Calendar to = Calendar.getInstance();
		to.setTime(toDate);

		if (date1.after(from) && date1.before(to)) {
			return false;
		}
		return true;

	}

	/**
	 * 开始时间特殊处理一下
	 *
	 * @param beginTime
	 * @return
	 */
	public static Date getBeginTime(Date beginTime) {
		Calendar date1 = Calendar.getInstance();
		date1.setTime(beginTime);
		date1.set(Calendar.MINUTE, 00);
		date1.set(Calendar.HOUR_OF_DAY, 00);
		date1.set(Calendar.SECOND, 00);
		return date1.getTime();
	}

	/**
	 * 结束时间特殊处理一下
	 *
	 * @param endTime
	 * @return
	 */
	public static Date getEndTime(Date endTime) {
		Calendar date1 = Calendar.getInstance();
		date1.setTime(endTime);
		date1.set(Calendar.MINUTE, 59);
		date1.set(Calendar.HOUR_OF_DAY, 23);
		date1.set(Calendar.SECOND, 59);
		return date1.getTime();
	}

	/**
	 * 返回当月第一天
	 */
	public static String getFirstDayOfMonth() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		str = sdf.format(lastDate.getTime());
		return str;
	}

	public static String getFirstDayOfYear() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.set(Calendar.MONTH, 0);// 设为1月
		str = sdf.format(lastDate.getTime());
		return str;
	}

	/**
	 * 获取当前时间的前后一天，为真前一天 为假后一天
	 */
	public static String getCurrentDate(boolean bool) {
		Calendar date1 = Calendar.getInstance();
		if (bool) {
			date1.add(Calendar.DATE, -1);
		} else {
			date1.add(Calendar.DATE, 1);
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date1.getTime());
	}

	/**
	 * 获取指定日期的前后一天，为真前一天 为假后一天
	 *
	 * @param bool
	 * @param currentDate
	 * @return
	 */
	public static String getCurrentDate(boolean bool, String currentDate) {
		Calendar date1 = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(currentDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		date1.setTime(date);

		if (bool) {
			date1.add(Calendar.DATE, -1);
		} else {
			date1.add(Calendar.DATE, 1);
		}

		return sdf.format(date1.getTime());
	}
	/**
	 * 根据时间获取上一年第一天 返回时间为 如：2018-01-01 00:00:00:000
	 * @param date
	 * @return
	 */
	public static Date getPreYearFirstDayTimeByDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR,-1);
		calendar.set(Calendar.MONTH,calendar.getActualMinimum(Calendar.MONTH));
		calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND,calendar.getActualMinimum(Calendar.MILLISECOND));
		return calendar.getTime();
	}
	/**
	 * 根据时间获取上一年最后天，比如2018年如果最后一个月有31 返回时间为 如：2018-12-31 23:59:59:999
	 * @param date
	 * @return
	 */
	public static Date getPreYearLastDayTimeByDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR,-1);
		calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
		int lastDay = 0;
		if(calendar.get(Calendar.MONTH)==2) {
			lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
		}else {
			lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		calendar.set(Calendar.DAY_OF_MONTH,lastDay);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND,calendar.getActualMaximum(Calendar.MILLISECOND));
		return calendar.getTime();
	}
	/**
	 * 根据时间获取指定月第一天 返回时间为 如：2018-01-01 00:00:00:000
	 * @param date 时间参数
	 * @param month 正数为后，负数为前
	 * @return
	 */
	public static Date getMonthFirstDayTimeByDate(Date date,int month){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH,month);
		calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND,calendar.getActualMinimum(Calendar.MILLISECOND));
		return calendar.getTime();
	}
	/**
	 * 根据时间获取指定月最后天，比如1月如果有31 返回时间为 如：2018-01-31 23:59:59:999
	 * @param date
	 * @param month  正数为后，负数为前
	 * @return
	 */
	public static Date getMonthLastDayTimeByDate(Date date,int month){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH,month);
		int lastDay = 0;
		if(calendar.get(Calendar.MONTH)==2) {
			lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
		}else {
			lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		calendar.set(Calendar.DAY_OF_MONTH,lastDay);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND,calendar.getActualMaximum(Calendar.MILLISECOND));
		return calendar.getTime();
	}

	/**
	 * 根据时间获取指定月第一天 返回时间为 如：2018-01-01 00:00:00:000
	 * @param date 时间参数
	 * @param year 正数为后，负数为前
	 * @return
	 */
	public static Date getYearFirstDayTimeByDate(Date date,int year){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR,year);
		calendar.add(Calendar.MONTH,calendar.getActualMinimum(Calendar.MONTH));
		calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND,calendar.getActualMinimum(Calendar.MILLISECOND));
		return calendar.getTime();
	}
	/**
	 * 根据时间获取指定月最后天，比如1月如果有31 返回时间为 如：2018-01-31 23:59:59:999
	 * @param date
	 * @param year  正数为后，负数为前
	 * @return
	 */
	public static Date getYearLastDayTimeByDate(Date date,int year){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR,year);
		calendar.add(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
		calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND,calendar.getActualMaximum(Calendar.MILLISECOND));
		return calendar.getTime();
	}
	/**
	 *  获取指定日期的前后月的第一天，为真当月第一天 为假下个月第一天
	 *
	 * @param bool
	 * @param currentDate
	 * @return
	 */
	public static String getCurrentMonth(boolean bool, String currentDate) {
		Calendar date1 = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(currentDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		date1.setTime(date);
		date1.set(Calendar.DATE, 1);

		if (bool) {

		} else {
			date1.add(Calendar.MONTH, 1);

		}

		return sdf.format(date1.getTime());
	}

	/**
	 * 获取指定日期的前后年的第一天，为真当年第一天 为假下年第一天
	 *
	 * @param bool
	 * @param currentDate
	 * @return
	 */
	public static String getCurrentYear(boolean bool, String currentDate) {
		Calendar date1 = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(currentDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		date1.setTime(date);
		date1.set(Calendar.DATE, 1);
		date1.set(Calendar.MONTH, 1);
		if (bool) {

		} else {
			date1.add(Calendar.YEAR, 1);
		}

		return sdf.format(date1.getTime());
	}

	/**
	 * 用途：以指定的格式格式化日期字符串
	 *
	 * @param pattern
	 *            字符串的格式
	 * @param currentDate
	 *            被格式化日期
	 * @return String 已格式化的日期字符串
	 * @throws NullPointerException
	 *             如果参数为空
	 */
	public static String formatDate(Object currentDate, String pattern) {
		if (currentDate == null || pattern == null) {
			throw new NullPointerException("The arguments are null !");
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(currentDate);
	}

	public static Date parseDate(String dateStr, String format) {
		Date date = null;
		try {
			date = new SimpleDateFormat(format).parse(dateStr);
		} catch (Exception e) {

			throw new IllegalArgumentException("日期转成时间报错");
		}
		return date;
	}

	/**
	 * 比较两个日期大小
	 *
	 * @param date1
	 *            日期字符串
	 * @param pattern1
	 *            日期格式
	 * @param date2
	 *            日期字符串
	 * @param pattern2
	 *            日期格式
	 * @return boolean 若是date1比date2小则返回true
	 * @throws ParseException
	 */
	public static boolean compareMinDate(String date1, String pattern1,
										 String date2, String pattern2) throws ParseException {
		try {
			Date d1 = parseDate(date1, pattern1);
			Date d2 = parseDate(date2, pattern2);
			return d1.before(d2);
		} catch (Exception ex) {
			return false;
		}
	}

	/**
	 * 比较两个日期大小
	 *
	 * @param date1
	 *            Date
	 * @param date2
	 *            Date
	 * @return boolean 若是date1比date2小则返回true
	 */
	public static boolean compareMinDate(Date date1, String pattern1,
										 Date date2, String pattern2) {
		try {
			return compareMinDate(formatDate(date1, pattern1), pattern1,
					formatDate(date2, pattern2), pattern2);
		} catch (Exception ex) {
			return false;
		}
	}

	/**
	 * 获取当前日期(yyyy-MM-dd)
	 *
	 * @return String型数据
	 */
	public static String getDay() {
		return getDay(FORMAT_YYYY_MM_DD);
	}

	/**
	 * 获取当前日期
	 *
	 * @param dateFormat
	 * @return String型数据
	 */
	public static String getDay(String dateFormat) {
		Calendar cal = Calendar.getInstance();
		if (dateFormat == null || !"".equals(dateFormat)) {
			dateFormat = FORMAT_YYYY_MM_DD;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		String mDateTime = formatter.format(cal.getTime());
		return mDateTime;
	}

	public static String getDateTime() {
		return getDateTime(FORMAT_YYYY_MM_DD);
	}

	public static String getDateTime(String dateFormat) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		String mDateTime = formatter.format(cal.getTime());
		return (mDateTime);
	}

	public static String getDateTime(long milliseconds) {
		SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
		return formatter.format(new Date(milliseconds));
	}

	/**
	 * 获取当前日期 （yyyy-MM-dd HH:mm:ss"）
	 *
	 * @return
	 */
	public static Date getDate() {
		return getDate(FORMAT_YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 根据传入格式获取当前日期
	 *
	 * @param dateFormat
	 * @return
	 */
	public static Date getDate(String dateFormat) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		String mDateTime = formatter.format(cal.getTime());
		return toDate(mDateTime, FORMAT_YYYY_MM_DD_HH_MM_SS);
	}
	/**
	 * 转换成日期型数据
	 *
	 * @param value
	 * @param dateFormat
	 * @return
	 */
	public static Date toDate(String value, String dateFormat) {

		Date d = null;
		SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
		try {
			d = dateFormatter.parse(value);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	/**
	 * 两个时间段的间隔
	 *
	 * @param startDate
	 * @param endDate
	 * @return 秒数
	 */
	public static Long getDateIntervalSecond(Date startDate, Date endDate) {
		Long sec = new Long("0");
		if (startDate != null) {
			if (endDate == null) {
				endDate = getDate();
			}
			sec = (Math.abs(startDate.getTime() - endDate.getTime())) / 1000;
		}
		return sec;
	}

	/**
	 * 获取两个时间段的间隔
	 *
	 * @param startDate
	 * @param endDate
	 * @return 分钟数
	 */
	public static Long getDateIntervalMinute(Date startDate, Date endDate) {
		Long sec = getDateIntervalSecond(startDate, endDate);
		Long minute = sec / 60;
		return minute;
	}

	/**
	 * 获取两个时间段的间隔
	 *
	 * @param startDate
	 * @param endDate
	 * @return 小时数
	 */
	public static Long getDateIntervalHour(Date startDate, Date endDate) {
		Long sec = getDateIntervalSecond(startDate, endDate);
		Long hour = sec / 3600;
		return hour;
	}

	/**
	 * 获取两个时间段的间隔
	 *
	 * @param startDate
	 * @param endDate
	 * @return 天数
	 */
	public static Long getDateIntervalDay(Date startDate, Date endDate) {
		Long sec = getDateIntervalSecond(startDate, endDate);
		Long day = sec / (24 * 3600);
		return day;
	}
	/**
	 * 获取日期间隔的天数（只计算到天）
	 * @param startDate
	 * @param endDate
	 */
	public static int getDateIntervalOnlyDay(Date startDate, Date endDate) {
		Calendar cStartDate = Calendar.getInstance();
		Calendar cEndDate = Calendar.getInstance();
		cStartDate.setTime(startDate);
		cEndDate.setTime(endDate);
		setTimeToMidnight(cStartDate);
		setTimeToMidnight(cEndDate);
		long todayMs = cStartDate.getTimeInMillis();
		long returnMs = cEndDate.getTimeInMillis();
		long intervalMs = returnMs - todayMs;
		return millisecondsToDays(intervalMs);
	}
	private static int millisecondsToDays(long intervalMs) {
		return (int) (intervalMs / (1000 * 86400));
	}
	private static void setTimeToMidnight(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
	}
	/**
	 * 获取两个时间段的间隔
	 *
	 * @param startDate
	 * @param endDate
	 * @return 月数
	 */
	@SuppressWarnings("deprecation")
	public static Long getDateIntervalMonth(Date startDate, Date endDate) {
		Long mon = new Long("0");
		if (startDate != null) {
			if (endDate == null) {
				endDate = getDate();
			}
			Date begin, end;
			if (startDate.getTime() > endDate.getTime()) {
				begin = endDate;
				end = startDate;
			} else {
				begin = startDate;
				end = endDate;
			}
			mon = new Long(
					((end.getYear() - begin.getYear()) * 12 + (end.getMonth() - begin
							.getMonth())));
		}
		return mon;
	}

	/**
	 * 获取两个时间段的间隔
	 *
	 * @param startDate
	 * @param endDate
	 * @return 毫秒数
	 */
	public static long getDateInterval(String startDate, String endDate) {
		Date d1 = null;
		Date d2 = null;
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			d1 = dateFormatter.parse(startDate);
			d2 = dateFormatter.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return getDateInterval(d1, d2);
	}

	/**
	 * 获取两个时间段的间隔
	 *
	 * @param startDate
	 * @param endDate
	 * @return 毫秒数
	 */
	public static long getDateInterval(Date startDate, Date endDate) {
		long day = (startDate.getTime() - endDate.getTime())
				/ (1000 * 60 * 60 * 24);
		return day;
	}

	/**
	 * 获取第N个星期
	 *
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {
		SimpleDateFormat weekFormat = new SimpleDateFormat("E");
		String returnValue = weekFormat.format(date);
		return Integer.parseInt(returnValue);
	}

	/**
	 * String日期转换成Date型日期(严格格式)
	 *
	 * @param string
	 * @param pattern
	 * @return
	 */
	public static Date formatStringToDateStrict(String string, String pattern) {
		return formatStrToDate(string, pattern, false);
	}

	/**
	 * String日期转换成Date型日期(非严格格式)
	 *
	 * @param string
	 * @param pattern
	 * @return
	 */
	public static Date formatStringToDateNoStrict(String string, String pattern) {
		return formatStrToDate(string, pattern, true);
	}

	public static boolean isValidateDate(String sDate) {
		if (null == sDate || "".equals(sDate)) {
			return false;
		}
		boolean flag = false;
		Pattern pattern0 = null;
		Matcher match0 = null;

		String datePattern = "("
				+
				// 第一种情况为月份是大月的有31天。
				"(^\\d{3}[1-9]|\\d{2}[1-9]\\d{1}|\\d{1}[1-9]\\d{2}|[1-9]\\d{3}"
				+ // 年
				"([-]?)"
				+ // 时间间隔符(-,/,.,_)
				"(10|12|0?[13578])"
				+ // 大月
				"([-]?)"
				+ // 时间间隔符(-,/,.,_)
				"((3[01]|[12][0-9]|0?[1-9])?)"
				+ // 日(31)要验证年月因此出现0/1次
				"([\\s]?)"
				+ // 空格
				"((([0-1]?[0-9]|2[0-3]):([0-5]?[0-9]):([0-5]?[0-9]))?))$"
				+ // 时分秒
				"|"
				+ // 或
				// 第二种情况为月份是小月的有30天，不包含2月。
				"(^\\d{3}[1-9]|\\d{2}[1-9]\\d{1}|\\d{1}[1-9]\\d{2}|[1-9]\\d{3}"
				+ // 年
				"([-]?)"
				+ // 时间间隔符(-,/,.,_)
				"(11|0?[469])"
				+ // 小月不含2月
				"([-]?)"
				+ // 时间间隔符(-,/,.,_)
				"(30|[12][0-9]|0?[1-9])"
				+ // 日(30)
				"([\\s]?)"
				+ // 空格
				"((([0-1]?[0-9]|2[0-3]):([0-5]?[0-9]):([0-5]?[0-9]))?))$"
				+ // 时分秒
				"|"
				+ // 或
				// 第三种情况为平年月份是2月28天的。
				"(^\\d{3}[1-9]|\\d{2}[1-9]\\d{1}|\\d{1}[1-9]\\d{2}|[1-9]\\d{3}"
				+ // 年
				"([-]?)"
				+ // 时间间隔符(-,/,.,_)
				"(0?2)"
				+ // 平年2月
				"([-]?)"
				+ // 时间间隔符(-,/,.,_)
				"(2[0-8]|1[0-9]|0?[1-9])"
				+ // 日(28)
				"([\\s]?)"
				+ // 空格
				"((([0-1]?[0-9]|2[0-3]):([0-5]?[0-9]):([0-5]?[0-9]))?))$"
				+ // 时分秒
				"|"
				+ // 或
				// 第四种情况为闰年月份是2月29天的。
				// 可以被4整除但不能被100整除的年份。
				// 可以被400整除的数亦是能被100整除，因此后两位是00，所以只要保证前两位能被4整除即可。
				"(^((\\d{2})(0[48]|[2468][048]|[13579][26]))|((0[48]|[2468][048]|[13579][26])00)"
				+ "([-]?)" + "(0?2)" + "([-]?)" + "(29)" + "([\\s]?)"
				+ "((([0-1]?\\d|2[0-3]):([0-5]?\\d):([0-5]?\\d))?))$" + // 时分秒
				")";
		;

		pattern0 = Pattern.compile(datePattern);
		match0 = pattern0.matcher(sDate);
		flag = match0.matches();
		return flag;
	}

	private static Date formatStrToDate(String string, String pattern,
										boolean isLenient) {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		if (pattern != null) {
			dateFormat.applyPattern(pattern);
		} else {
			dateFormat.applyPattern(FORMAT_YYYY_MM_DD);
		}
		if ((string == null) || (string.equals(""))) {
			return null;
		} else {
			try {
				dateFormat.setLenient(isLenient);
				return dateFormat.parse(string);
			} catch (ParseException e) {
				return null;
			}
		}
	}

	/**
	 * Date型日期转换成String日期
	 *
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDateToStringWithNull(Date date, String pattern) {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		if (pattern != null) {
			dateFormat.applyPattern(pattern);
		} else {
			dateFormat.applyPattern(FORMAT_YYYY_MM_DD);
		}
		if (date == null) {
			return null;
		} else {
			return dateFormat.format(date);
		}

	}

	/**
	 * 转换成秒
	 *
	 * @param day
	 * @param hour
	 * @param minutes
	 * @return
	 */
	public static int convert2Second(int day, int hour, int minutes) {
		int seconds = 0;
		if (day > 0) {
			seconds += 24 * 60 * 60 * day;
		}
		if (hour > 0) {
			seconds += 60 * 60 * hour;
		}
		if (minutes > 0) {
			seconds += 60 * minutes;
		}
		return seconds;
	}

	/**
	 * 计算指定天数前或后的当前时间
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date addDate(Date date,long day) {
		long time = date.getTime(); // 得到指定日期的毫秒数
		day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
		time+=day; // 相加得到新的毫秒数
		Date newDate = new Date(time);
		return newDate; // 将毫秒数转换成日期
	}
	/**
	 * 加减去相应的数量的天的日期
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date adddays(Date date,int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		Date newTime = calendar.getTime();
		return newTime;
	}
	/**
	 * 加减去相应的数量的月份的日期
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date addMonths(Date date,int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		Date newTime = calendar.getTime();
		return newTime;
	}
	/**
	 * 加减去相应的数量的年的日期
	 * @param date
	 * @param year
	 * @return
	 */
	public static Date addYears(Date date,int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		Date newTime = calendar.getTime();
		return newTime;
	}


	/**
	 *  根据起始结束数据获取间隔时间段内的日期集合,不包含结束时间
	 * @param startDate
	 * @param endDate
	 * @return  ['YYYY-MM-DD','YYYY-MM-DD'...]
	 */
	public static List<String> getDateStrList(Date startDate,Date endDate) {
		List<String> dataList = new ArrayList<String>();
		dataList.add(DateUtil.formatDate(startDate,DateUtil.FORMAT_YYYY_MM_DD));
		Calendar calend = Calendar.getInstance();
		calend.setTime(startDate);
		calend.add(Calendar.DATE, 1);
		Date curDate = calend.getTime();
		while (curDate.getTime()<endDate.getTime()){
			dataList.add(DateUtil.formatDate(curDate,DateUtil.FORMAT_YYYY_MM_DD));
			calend.setTime(curDate);
			calend.add(Calendar.DATE, 1);
			curDate = calend.getTime();
		}
		return dataList;
	}
	public static void main(String[] args){

	    //String machineCode= "19SJ200012001001,19SJ200012001002,19SJ200012001003,19SJ200012001004,19SJ200012001005,19SJ200012001006,19SJ200012001007,19SJ200012001008,19SJ200012001009,19SJ200012001010,19SJ200012001011,19SJ200012001012,19SJ200012001013,19SJ200012001014,19SJ200012001015,19SJ200012001016,19SJ200012001017,19SJ200012001018,19SJ200012001019,19SJ200012001020,19SJ200012001021,19SJ200012001022,19SJ200012001023,19SJ200012001024,19SJ200012001025,19SJ200012001026,19SJ200012001027,19SJ200012001028,19SJ200012001029,19SJ200012001030,19SJ200012001031,19SJ200012001032,19SJ200012001033,19SJ200012001034,19SJ200012001035,19SJ200012001036,19SJ200012001037,19SJ200012001038,19SJ200012001039,19SJ200012001040";
        //String[] machineCodeArr = machineCode.split(",");
        //for(int i=0;i<machineCodeArr.length;i++){
        //    System.out.println("INSERT INTO `zheng`.`umbrella_hardware_machine` ( `code`, `name`, `capacity`, `type`, `status`, `faultstatus`, `launchtime`, `agent_id`, `country`, `province`, `city`, `address`, `oem_id`, `longitude`, `latitude`, `description`, `flag`, `cuser`, `ctime`, `remainder`, `rent_rule_id`, `longitude_report`, `latitude_report`, `lasttime_report`, `electricQuantity`, `rule_deposit_id`, `pathcount`) VALUES ( '"+machineCodeArr[i]+"', '二代机', '40', '2', '3', '0', '1574175764361', '27', '中国', '广东', '广州', '', NULL, NULL, NULL, '', '1', 'admin', '1574175764361', '0', '2', NULL, NULL, NULL, '100.00', NULL, '1');");
        //    System.out.println("INSERT INTO `zheng`.`umbrella_hardware_path` ( `agent_id`, `name`, `code`, `number`, `machine_code`, `capacity`, `remainder`, `status`, `type`, `description`, `flag`, `cuser`, `ctime`) VALUES ( '27', '伞道1', '"+machineCodeArr[i]+"01', '1', '"+machineCodeArr[i]+"', '40', '0', '1', '1', NULL, '1', 'admin', '1574175764361');");
        //}
        Date endTime = DateUtil.getNowDate();
		System.out.println("----"+DateUtil.date2Str(DateUtil.adddays(endTime, 62),FORMAT_YY));
		Date beginDate = DateUtil.getBeginTime(DateUtil.adddays(endTime, 2) );
		Date endDate = DateUtil.getBeginTime(DateUtil.adddays(endTime, 3) );
		String xxx = DateUtil.date2Str(DateUtil.addDate(endTime, 3),DateUtil.FORMAT_YYYY_MM_DD);
		List<String> list = getDateStrList(DateUtil.parseDate("2019-01-12",DateUtil.FORMAT_YYYY_MM_DD),
				DateUtil.parseDate("2019-02-12",DateUtil.FORMAT_YYYY_MM_DD));
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
//
		//System.out.println(getDateIntervalOnlyDay(DateUtil.parseDate("2019-02-12",DateUtil.FORMAT_YYYY_MM_DD),DateUtil.parseDate("2019-02-11",DateUtil.FORMAT_YYYY_MM_DD)));

	}
}

